package cn.cloudheritagefeign.service;

import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Component;

@Component // 将其声明为Spring Bean，以便被Spring管理和注入
public class HeritageFeignClientFallback implements HeritageFeignClient {

    @Override
    public ResponseEntity<String> testLoadBalancer() {
        // 当 inventory-warning-service 的 testLoadBalancer 方法调用失败或熔断时，会执行此方法
        String fallbackMessage = "CIRCUIT BREAKER TRIPPED! Fallback Response: Inventory service is temporarily unavailable. Please try again later. (Port: N/A)";
        return new ResponseEntity<>(fallbackMessage, HttpStatus.SERVICE_UNAVAILABLE);
    }

    // 如果 InventoryWarningFeignClient 中有其他方法，也需要在这里提供对应的降级实现
    @Override
    public ResponseEntity<Object> getProductById(Long id) {
        String fallbackMessage = String.format("CIRCUIT BREAKER TRIPPED! Fallback Response: Unable to retrieve product with ID %d. Inventory service is temporarily unavailable.", id);
        // 你可以返回一个空的DTO对象或者null，取决于你的API设计
        // 这里我们返回null作为body，并设置HTTP状态码
        return new ResponseEntity<>(fallbackMessage, HttpStatus.SERVICE_UNAVAILABLE);
    }

    // 例如:
    // @Override
    // public ResponseEntity<List<StorageAreaNameDto>> getDistinctStorageAreas() {
    //     return new ResponseEntity<>(Collections.emptyList(), HttpStatus.SERVICE_UNAVAILABLE);
    // }
}
